package com.financing.wap.web.controller.adminapi;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstMemberSendLogDTO;
import com.financing.wap.dto.DstUserArticleDTO;
import com.financing.wap.dto.DstUserInfoDTO;
import com.financing.wap.dto.DstUserWithdrawLogDTO;
import com.financing.wap.service.ICommonService;
import com.financing.wap.service.IDstMemberSendLogService;
import com.financing.wap.service.IDstUserInfoService;
import com.financing.wap.service.IDstUserWithdrawLogService;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.admin.AdminUserInfoVO;
import com.financing.wap.vo.admin.AdminUserInvitationVO;
import com.financing.wap.vo.admin.AdminUserVO;
import com.financing.wap.web.controller.BaseController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

@Controller("adminDstUserInfoController")
@RequestMapping("/adminapi/dstUserInfo")
public class DstUserInfoController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstUserInfoService dstUserInfoService;
	
	@Autowired
	private ICommonService commonService;

    @Autowired
    private IDstMemberSendLogService dstMemberSendLogService;

    @Autowired
    private IDstUserWithdrawLogService dstUserWithdrawLogService;
	
	/***
	 * 用户详情页-基本信息
	 * @param request
	 * @param dstUserInfo
	 * @return
	 */
	@RequestMapping("/detailed")
	@ResponseBody
	public ResultMap detailed(HttpServletRequest request, DstUserInfoDTO dstUserInfo){

		ResultMap result = new ResultMap();
		try{

			logger.info("query admin user detailed begin .......");

			String openid = request.getParameter("openid");

			if(StringUtil.isNullOrEmptyStr(openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			//result.setAttribute("list", commonDstService.userInvitationList(openid));

            //基本信息
            AdminUserInfoVO adminUserInfoVO = commonService.queryUserInfoDetailed(openid);

            //会员开通记录
            DstMemberSendLogDTO dstMemberSendLog = new DstMemberSendLogDTO();
            dstMemberSendLog.setUserOpenid(openid);
            List<DstMemberSendLogDTO> memberLogList = dstMemberSendLogService.queryList(dstMemberSendLog);

            //兑换和提现
            DstUserWithdrawLogDTO userWithdrawLogDTO = new DstUserWithdrawLogDTO();
            userWithdrawLogDTO.setUserOpenid(openid);
            List<DstUserWithdrawLogDTO> withdrawLogList = dstUserWithdrawLogService.queryList(userWithdrawLogDTO);

            //会员列表
            List<AdminUserInvitationVO> memberList =  commonService.adminUserInvitationList(openid);

            result.setAttribute("bean", adminUserInfoVO);
            result.setAttribute("memberLogList", memberLogList);
            result.setAttribute("withdrawLogList", withdrawLogList);
            result.setAttribute("memberList", memberList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query userInvitationList list end .......");

		}catch(Exception e){
			log.error("query userInvitationList error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}

    /***
     * 用户详情页-基本信息
     * @param request
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResultMap list(HttpServletRequest request, AdminUserVO adminUserVO,Pager pager){

        ResultMap result = new ResultMap();
        try{

            logger.info("query userInfo list begin .......");

            adminUserVO.setBegin(pager.getStartIndex());
            adminUserVO.setPageSize(pager.getPageSize());

            int count = commonService.queryUserInfoListCount(adminUserVO);

            pager.setTotalCount(count);

            List<AdminUserVO> adminUserList = null;

            if(count > 0){
                adminUserList = commonService.queryUserInfoList(adminUserVO);
            }else{
                adminUserList = Collections.emptyList();
            }

            result.setAttribute("pager", pager);
            result.setAttribute("list", adminUserList);
            result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
            result.setStatus(ResultMap.STATUS.SUCCESS);

            logger.info("query userInfo list end .......");
        }catch(Exception e){
            log.error("query userInfo error!",e);
            result.setStatus(ResultMap.STATUS.ERROR);
            result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
        }
        return result;
    }
	
	
}
